package com.kkb.shopcenter.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.shopcenter.api.UserInfoService;
import com.kkb.shopcenter.common.common.HttpResult;
import com.kkb.shopcenter.common.common.ResultEnum;
import com.kkb.shopcenter.common.exception.DefinitionException;
import com.kkb.shopcenter.common.pojo.UserInfo;
import io.swagger.annotations.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "用户管理", description = "用户管理")
@RestController
@RequestMapping("/api/users")
public class UserInfoController {

    @Reference
    private UserInfoService userInfoService;

    @GetMapping("/allUser")
    public HttpResult getAllUserInfo(){

        return HttpResult.success(userInfoService.getAllUserInfo());
    }

    @PostMapping("/addUser")
    public HttpResult addUserInfo(@RequestBody UserInfo userInfo){

        return HttpResult.success(userInfoService.addUserInfo(userInfo));
    }

    @PutMapping("/updateUser")
    public HttpResult updateUserInfo(@RequestBody UserInfo userInfo){

        return HttpResult.success(userInfoService.updateUserInfo(userInfo));
    }

    @DeleteMapping("/delUserById/{id}")
    public HttpResult deleteUserById(@PathVariable Long id){

        return  HttpResult.success(userInfoService.deleteUserById(id));
    }
    @ApiOperation(value = "用户分页查询",notes = "接受分页参数page,pageSize")
    @GetMapping("/userList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "page", value = "第几页", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页显示多少条数据", required = true, dataType = "int")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "条件分页查询用户成功"),
            @ApiResponse(code = 500, message = "条件分页查询用户失败,后台服务出现异常"),
            @ApiResponse(code = 401, message = "代表此操作无权限访问"),
            @ApiResponse(code = 400, message = "表示请求参数错误"),
    })
    public HttpResult getPageUserInfo(@RequestParam(value = "page",defaultValue = "1") int pageNum,
                                              @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        PageInfo<UserInfo> pageInfo =  userInfoService.getPageUserInfo(pageNum+1,pageSize);
        return HttpResult.success(pageInfo);
    }



}
